/******************************************************************************
 * FileName:    gwin_security.c
 * Desc:        安全
 *              广云秘钥：
 *                  连接类：
 *                      1.计算秘钥序列号：秘钥序列号 = (蓝牙数据包id(随机数) ^ mac地址倒数第二字节) % 秘钥字典总长度
 *                      2.在秘钥字典中偏移秘钥序列号数值，取n个字节长度（待解密数据长度），若超出最后一个字节继续从第一个字节取值
 *                      3.待解密数据与秘钥按字节顺序异或计算： 解密数据 = 待解密数据 ^ 秘钥
 *
 *
 * Author:      Lin
 * Notes:
 *
 ******************************************************************************/

/*-------------------------- Includes ---------------------------------------*/
#include "gwin_security.h"


/*-------------------------- Macro Definitions ------------------------------*/

/*-------------------------- Type Declarations ------------------------------*/

/*-------------------------- Global Variables -------------------------------*/

/*-------------------------- Local Variables --------------------------------*/
/**
 * @brief 广云ComboV3秘钥字典
 */
static const uint8_t gwin_security_combo_v3_key_map[] =
{
    0xE2, 0x67, 0x57, 0x84, 0x3F, 0x42, 0x3B, 0x58, 0xB0, 0xF1,
    0xBC, 0xC2, 0x7B, 0xE4, 0xD1, 0x2A, 0x13, 0x64, 0xC3, 0xDB,
    0xBD, 0xB0, 0xC6, 0x18, 0x8A, 0xED, 0x73, 0xBB, 0x3D, 0x98,
    0x43, 0x92, 0xB0, 0xB3, 0x8C, 0x30, 0xB6, 0x9C, 0x11, 0xA1,
    0xB7, 0x45, 0x89, 0xCF, 0x11, 0x17, 0x2F, 0xD3, 0x46, 0xB1,
    0xB6, 0x8D, 0xE2, 0x04, 0x5D, 0x6A, 0x69, 0x18, 0xE2, 0x02,
    0x32, 0x59, 0xB0, 0xA1, 0x33, 0xB2, 0xB6, 0x91, 0xC9, 0xAA,
    0xB4, 0x9E, 0x9E, 0x71, 0xAF, 0x3A, 0x5E, 0xD0, 0xEF, 0xFE,
    0x58, 0x93, 0x38, 0x1B, 0xA1, 0xFE, 0x11, 0x30, 0x6C, 0x6D,
    0xE6, 0x07, 0x86, 0x93, 0x6D, 0x82, 0x0C, 0x36, 0x24, 0x00,
    0x3D, 0x00, 0x29, 0xDB, 0x81, 0xD2, 0x77, 0xEF, 0x2A, 0xC9,
    0x23, 0xF4, 0x50, 0x85, 0x55, 0x07, 0x92, 0x37, 0x18, 0x7C,
    0xC7, 0xA5, 0x5F, 0x0B, 0xE7, 0xA1, 0x5F, 0x95, 0xC0, 0x8D,
    0x23, 0x03, 0x81, 0x7E, 0x2D, 0x52, 0x32, 0xFE, 0x72, 0xB7,
    0xDE, 0x01, 0xA3, 0x49, 0x79, 0x4B, 0x04, 0xB5, 0xB3, 0xC4,
    0xC3, 0xC8, 0xB0, 0x71, 0x90, 0x2C, 0x6E, 0xB5, 0x0F, 0x4C,
    0x13, 0x18, 0xE7, 0xBE, 0x68, 0xA1, 0x83, 0xAF, 0x2F, 0xC2,
    0xBA, 0x40, 0x9C, 0x58, 0x25, 0x1A, 0x50, 0x35, 0x5A, 0xA5,
    0x4E, 0xE4, 0x77, 0xF0, 0xFD, 0x90, 0xC0, 0x43, 0x6D, 0x0E,
    0x26, 0x91, 0x8B, 0xBB, 0x65, 0xFA, 0xB3, 0xBF, 0x44, 0xD9,
    0x2E, 0xA5, 0x78, 0xC6, 0x34, 0xAF, 0x6A, 0x5F, 0x03, 0x63,
    0x4E, 0x10, 0xE2, 0xF8, 0xD9, 0x95, 0xE6, 0x83, 0x01, 0xCB,
    0x39, 0x0B, 0x31, 0xD8, 0x5C, 0xF2, 0x83, 0xBD, 0x35, 0x4C,
    0x5D, 0x98, 0x21, 0x12, 0x8A, 0x9C, 0xF6, 0x01, 0xE3, 0x51,
    0xA7, 0x14, 0x13, 0xD1, 0xFD, 0xF3, 0x41, 0x68, 0x50, 0x02,
    0x03, 0x94, 0x81, 0xA7, 0x72, 0xBB,
};
static const uint16_t gwin_security_combo_v3_key_map_len = sizeof(gwin_security_combo_v3_key_map);


/**
 * @brief 广云公版秘钥字典
 */
static const uint8_t gwin_security_public_key_map[] =
{
    0x85, 0xEF, 0x03, 0x04, 0xE8, 0x82, 0x40, 0x18, 0x8B, 0x6F,
    0x65, 0x89, 0x50, 0x04, 0x88, 0xA2, 0x84, 0x5C, 0x8A, 0x13,
    0x59, 0x7D, 0x3A, 0x84, 0x06, 0x12, 0x80, 0x04, 0x25, 0x78,
    0xCA, 0x6B, 0x27, 0xE1, 0xB8, 0x62, 0x7A, 0xF2, 0x50, 0x0C,
    0x00, 0xF9, 0xFB, 0x76, 0xD2, 0x53, 0x5F, 0x79, 0xB5, 0x85,
    0xA2, 0x9E, 0xA6, 0x98, 0xA4, 0x2B,
};
static const uint16_t gwin_security_public_key_map_len = sizeof(gwin_security_public_key_map);


/*-------------------------- Functions Declarations -------------------------*/

/*-------------------------- Functions Definitions --------------------------*/
/**
 * @brief 解密数据
 */
static void gwin_security_decrypted_data(const uint8_t *key_map, uint16_t key_map_len, uint16_t private_key_offset, const uint8_t *encrypted_data, uint8_t *decrypted_data, uint16_t len)
{
    uint16_t private_key_pos;

    for (uint16_t i = 0; i < len; i++)
    {
        private_key_pos = (private_key_offset + i) % key_map_len;
        decrypted_data[i] = encrypted_data[i] ^ key_map[private_key_pos];
    }
}

/**
 * @brief 解密数据
 *          广云ComboV3
 */
void gwin_security_decrypted_data_combo_v3(uint8_t fixed_id, uint8_t rand_id, const uint8_t *encrypted_data, uint8_t *decrypted_data, uint16_t len)
{
    gwin_security_decrypted_data(gwin_security_combo_v3_key_map, gwin_security_combo_v3_key_map_len, fixed_id ^ rand_id, encrypted_data, decrypted_data, len);
}

/**
 * @brief 解密数据
 *          广云公版秘钥字典
 */
void gwin_security_decrypted_data_public_key(uint8_t fixed_id, uint8_t rand_id, const uint8_t *encrypted_data, uint8_t *decrypted_data, uint16_t len)
{
    gwin_security_decrypted_data(gwin_security_public_key_map, gwin_security_public_key_map_len, fixed_id ^ rand_id, encrypted_data, decrypted_data, len);
}


/*-------------------------- END OF FILE ------------------------------------*/

